package com.cf.main;

import java.util.ArrayList;
import java.util.List;

public class GetScore implements Base {
	
	public static double [][]getScore(int[][] user_movie_base,double[][] similarityMatrix ){
	
	double[][] predictMatrix = new double[USERSIZE][ITEMSIZE];
	// 进行评分预测
	List<Neighbor> neighborSerial = new ArrayList<Neighbor>();
	for (int i = 0; i < USERSIZE; i++) {
		neighborSerial.clear();
		// 该方法有三个参数，用户编号，用户-项目评分表；similarityMatrix表示用户相似性矩阵
		neighborSerial = new FindKNeighbors().findKNeighbors(i, user_movie_base, similarityMatrix);
		for (int j = 0; j < ITEMSIZE; j++) {
			if (user_movie_base[i][j] == 0) {
				double similaritySum = 0;
				double sum = 0;
				double score = 0;
				for (int m = 0; m < neighborSerial.size(); m++) {
					//与邻居的相关系数*邻居对该项目的评分
					sum += Math.abs(neighborSerial.get(m).getValue() * user_movie_base[neighborSerial.get(m).getID()][j]);
					similaritySum += Math.abs(neighborSerial.get(m).getValue());
				}
				
				if (equal(similaritySum, 0.0d))
					score = 0.0d;
				else
					score = sum / similaritySum;
				predictMatrix[i][j] = score;
			} else {
				predictMatrix[i][j] = 0.0d;
			}
		}
	}
	return predictMatrix;
	}
	
	static boolean equal(double num1, double num2) {
		if ((num1 - num2 > -0.000001) && (num1 - num2) < 0.000001)
			return true;
		else
			return false;
	}
}
